<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------

namespace app\user\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use app\user\model\Payment as PaymentModel;

/**
 * 支付种类控制器
 * @package app\user\admin
 */
class Payment extends Admin
{
    /**
     * 支付种类列表
     * @author 李洪波 <361951119@qq.com>
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $data_list = PaymentModel::where($this->getMap())->where(['is_delete'=>0])
            ->order($this->getOrder('id DESC'))
            ->paginate();

        return ZBuilder::make('table')
            ->hideCheckbox()
            ->setTableName('payment')
            ->addTopButton('add',['title'=>'Add'])
            ->addRightButton('edit',['title'=>'edit'])
            ->addRightButton('delete',['title'=>'delete','data-confirm'=>'confirm','data-cancel'=>'cancel','data-title'=>'Warning! After deleting data, merchants associated with that data will need to reset the payment data, sure you want to continue?'])
            ->addColumns([
                ['id', 'ID'],
//                ['name', 'Payment'],
                ['icon', 'Icon','picture'],
                ['status', 'Status','switch'],
                ['update_user', 'Updater'],
                ['update_time', 'Last update time'],
                ['right_button', 'Operation', 'btn'],
            ])
            ->setRowList($data_list)
            ->setSearchArea([
                ['select', 'status', 'Status', '', '', ['disable','enable']]
            ])
            ->fetch();
    }

    /**
     * 新增
     * @author 蔡伟明 <314013107@qq.com>
     * @return mixed
     * @throws \think\Exception
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();

//            $data['name'] == '' && $this->error('Please fill in the payment name');
//            if(!$this->check($data['name'])) $this->error('Data duplicated, please re-enter');
            $data['icon'] == '' && $this->error('Please upload icon');
            $list = [];
//            $list['name']=$data['name'];
            $list['status']=isset($data['status'])?1:0;
            $list['icon']=$data['icon'];
            $list['update_time']=date('Y-m-d H:i:s');
            $list['create_time']=date('Y-m-d H:i:s');
            $list['update_user']=session('user_auth')['username'];
            $paymentModel = new PaymentModel();
            if (false !== $paymentModel->save($list)) {
                $this->success('add success', 'index');
            } else {
                $this->error('add failed');
            }
        }

        return ZBuilder::make('form')
            ->addFormItems([
//                ['text', 'name', 'Payment'],
                ['image', 'icon', 'Icon'],
                ['switch','status','Status','',1]
            ])
            ->fetch();
    }

    public function edit($id = 0,$pid=0)
    {
        if ($id === 0) $this->error('missing parameter');

        // 保存数据
        if ($this->request->isPost()) {
            $data = $this->request->post('', null, 'trim');
            // 验证
//            $data['name'] == '' && $this->error('Please fill in the category name');
//            if(!$this->check($data['name'],$id)) $this->error('Data duplicated, please re-enter');
            $data['icon'] == '' && $this->error('Please upload icon');
            $data['status']=isset($data['status'])?1:0;
            $data['update_time']=date('Y-m-d H:i:s');
            $data['update_user']=session('user_auth')['username'];
            if (PaymentModel::update($data,['id'=>$id])) {
                $this->success('edit success', 'index');
            } else {
                $this->error('edit failed');
            }
        }
        // 获取数据
        $info = PaymentModel::get($id);

        // 使用ZBuilder快速创建表单
        return ZBuilder::make('form')
            ->addFormItems([
//                ['text', 'name', 'Payment'],
                ['image', 'icon', 'Icon'],
                ['switch','status','Status','',1]
            ])
            ->setFormData($info)
            ->fetch();
    }


    public function delete($record = [])
    {
        $ids   = $this->request->isPost() ? input('post.ids/a') : input('param.ids');
        if (PaymentModel::update(['is_delete'=>1],['id'=>$ids])) {
            $this->success('delete success', 'index');
        } else {
            $this->error('delete failed');
        }
    }

    private function check($name,$id=0){
        $id=PaymentModel::where(['name'=>$name])
            ->when($id,function ($query)use($id){
                $query->where('id','neq',$id);
            })
            ->value('id');
        if($id){
            return false;
        }else{
            return true;
        }
    }
}
